package com.megalith.utils;

import com.megalith.vo.Result;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author lfp
 * @Date 2022/7/16
 * @Version 1.0
 */
public class RequestResponseUtil {

    /**
     * 处理错误响应信息
     */
    public static Result doBindingResultHandel(BindingResult result){
        List errors = new ArrayList<String>();
        List<ObjectError> allErrors = result.getAllErrors();
        // 遍历错误信息，返回给前端
        for (ObjectError error : allErrors) {
            errors.add(error.getDefaultMessage());
        }
        return Result.error(errors);
    }
}
